import { _decorator, Component, Node, math, Vec3, v3 } from 'cc';
import { Events } from '../events/Events';
import { VirtualInput } from '../input/VirtualInput';
import { ActorManager } from '../level/ActorManager';
import { MathUtil } from '../utils/MathUtil';
import { Actor } from './Actor';
import { ProjectileEmitter } from './ProjectileEmitter';
import { EState } from './StateDefine';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('PlayerController')
@requireComponent(Actor)
export class PlayerController extends Component {
    private actor: Actor = null
    @property(Node)
    private bowString: Node = null

    private splitAngle: number[] = [0]
    start() {
        this.actor = this.node.getComponent(Actor)
        this.projectileCount = this.actor.actorProp.projectileCount
        this.node.on("onFrameAttackLoose", this.onFrameAttackLoose, this)
        this.node.on(Events.OnKill, this.onKill, this)
        ActorManager.instance.playerActor = this.actor
    }
    onDestory() {
        ActorManager.instance.playerActor = null
    }
    update(deltaTime: number) {
        this.actor.input.x = VirtualInput.horizontal
        this.actor.input.z = -VirtualInput.vertical
        if (this.actor.input.length() > 0) {
            this.actor.changState(EState.Run)
        } else {
            let enemy = this.getNearEnemy()
            if (enemy == null) {
                this.actor.changState(EState.Idle)
            } else {               
                //转向敌人的方向
                Vec3.subtract(this.actor.input, enemy.worldPosition, this.node.worldPosition)
                this.actor.input.y = 0
                this.actor.input.normalize()
                this.actor.changState(EState.Attack)
            }
        }
    }
    private onFrameAttackLoose() {
        //发射箭矢
        const arrowStartPos = this.bowString.worldPosition

        let arrowForward: Vec3 = new Vec3()

        for (let i = 0; i < this.actor.actorProp.projectileCount; i++) {
            //罗德里格旋转公式(扇形发射)
            MathUtil.rotateAround(arrowForward, this.node.forward, Vec3.UP, this.splitAngle[i])
            let emitter = this.node.getComponent(ProjectileEmitter)
            let projectile = emitter.create()
            projectile.startTime = 0
            projectile.host = this.node
            projectile.node.worldPosition = arrowStartPos
            projectile.node.forward = arrowForward
        }
    }

    public set projectileCount(count: number) {
        //添加不同角度的箭矢
        this.splitAngle = []
        const rad = math.toRadian(10)
        const isOdd = count % 2 != 0
        const len = Math.floor(count / 2)
        for (let i = 0; i < len; i++) {
            this.splitAngle.push(-rad * (i + 1))
            this.splitAngle.push(rad * (i + 1))
        }
        if (isOdd) {
            this.splitAngle.push(0)
        }
    }

    //得到最近的敌人
    public getNearEnemy() {
        let property = this.actor.actorProp
        let minDistance = property.attackDistance
        let minNode: Node = null
        for (let enemy of ActorManager.instance.enemies) {
            let distance = Vec3.distance(this.node.worldPosition, enemy.worldPosition)
            if (distance < minDistance) {
                minDistance = distance
                minNode = enemy
            }

        }
        return minNode
    }
    private onKill() {
        let property = this.actor.actorProp
        property.exp++
        if (property.exp >= property.maxExp) {
            property.exp = 0
            property.maxExp *= 1.2
            property.level++
            this.node.emit(Events.OnPlayerUpgrade, property.level)
        }
        this.node.emit(Events.OnExpGain, property.exp, property.maxExp)
    }
}


